package com.xinzhidi.web.netty.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinzhidi.common.core.domain.model.LoginUser;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.utils.DateUtils;
import com.xinzhidi.common.utils.SecurityUtils;
import com.xinzhidi.common.utils.StringUtils;
import com.xinzhidi.common.utils.bean.BeanUtils;
import com.xinzhidi.web.netty.entity.AgreementHouseholdValveHistory;
import com.xinzhidi.web.netty.entity.dto.AgreementHouseholdValveHistoryAddDto;
import com.xinzhidi.web.netty.entity.dto.AgreementHouseholdValveHistoryListDto;
import com.xinzhidi.web.netty.entity.dto.AgreementHouseholdValveHistoryUpdDto;
import com.xinzhidi.web.netty.entity.vo.AgreementHouseholdValveHistoryInfoVo;
import com.xinzhidi.web.netty.entity.vo.AgreementHouseholdValveHistoryListVo;
import com.xinzhidi.web.netty.mapper.AgreementHouseholdValveHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;


/**
 * 户用阀门协议-历史数据Service业务层处理
 *
 * @author cx
 * @date 2023-06-16
 */
@Service
public class AgreementHouseholdValveHistoryService extends ServiceImpl<AgreementHouseholdValveHistoryMapper, AgreementHouseholdValveHistory> {

    @Autowired
    private AgreementHouseholdValveHistoryMapper agreementHouseholdValveHistoryMapper;

    /**
     * 查询户用阀门协议-历史数据
     *
     * @param id 户用阀门协议-历史数据主键
     * @return 户用阀门协议-历史数据
     */
    public AgreementHouseholdValveHistoryInfoVo selectAgreementHouseholdValveHistoryById(Long id) {
        AgreementHouseholdValveHistory agreementHouseholdValveHistory = agreementHouseholdValveHistoryMapper.selectById(id);
        AgreementHouseholdValveHistoryInfoVo infoVo = new AgreementHouseholdValveHistoryInfoVo();
        BeanUtils.copyProperties(agreementHouseholdValveHistory, infoVo);
        return infoVo;
    }

    /**
     * 查询户用阀门协议-历史数据列表
     *
     * @param dto 查询户用阀门协议-历史数据dto
     * @return 户用阀门协议-历史数据
     */
    public TableDataInfo<AgreementHouseholdValveHistoryListVo> selectAgreementHouseholdValveHistoryList(AgreementHouseholdValveHistoryListDto dto) {
        LambdaQueryWrapper<AgreementHouseholdValveHistory> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(dto.getInstructName()), AgreementHouseholdValveHistory::getInstructName, dto.getInstructName());
        wrapper.like(StringUtils.isNotEmpty(dto.getAddress()), AgreementHouseholdValveHistory::getAddress, dto.getAddress());
        wrapper.orderByDesc(AgreementHouseholdValveHistory::getId);
        return TableDataInfo.getDataTable(agreementHouseholdValveHistoryMapper.selectList(wrapper), (domain -> {
            AgreementHouseholdValveHistoryListVo vo = new AgreementHouseholdValveHistoryListVo();
            BeanUtils.copyProperties(domain, vo);
            return vo;
        }));
    }

    /**
     * 新增户用阀门协议-历史数据
     *
     * @param dto 新增户用阀门协议-历史数据dto
     * @return 结果
     */
    public int insertAgreementHouseholdValveHistory(AgreementHouseholdValveHistoryAddDto dto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        AgreementHouseholdValveHistory agreementHouseholdValveHistory = new AgreementHouseholdValveHistory();
        BeanUtils.copyProperties(dto, agreementHouseholdValveHistory);
        agreementHouseholdValveHistory.setCreateBy(loginUser.getUserId().toString());
        agreementHouseholdValveHistory.setCreateTime(DateUtils.getNowDate());
        return agreementHouseholdValveHistoryMapper.insert(agreementHouseholdValveHistory);
    }

    /**
     * 修改户用阀门协议-历史数据
     *
     * @param dto 修改户用阀门协议-历史数据dto
     * @return 结果
     */
    public int updateAgreementHouseholdValveHistory(AgreementHouseholdValveHistoryUpdDto dto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        AgreementHouseholdValveHistory agreementHouseholdValveHistory = new AgreementHouseholdValveHistory();
        BeanUtils.copyProperties(dto, agreementHouseholdValveHistory);
        agreementHouseholdValveHistory.setUpdateBy(loginUser.getUserId().toString());
        agreementHouseholdValveHistory.setUpdateTime(DateUtils.getNowDate());
        return agreementHouseholdValveHistoryMapper.updateById(agreementHouseholdValveHistory);
    }

    /**
     * 批量删除户用阀门协议-历史数据
     *
     * @param ids 需要删除的户用阀门协议-历史数据主键
     * @return 结果
     */
    public int deleteAgreementHouseholdValveHistoryByIds(Long[] ids) {
        return agreementHouseholdValveHistoryMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除户用阀门协议-历史数据信息
     *
     * @param id 户用阀门协议-历史数据主键
     * @return 结果
     */
    public int deleteAgreementHouseholdValveHistoryById(Long id) {
        return agreementHouseholdValveHistoryMapper.deleteById(id);
    }
}
